import Vue from 'vue'
import VueRouter from 'vue-router'
// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
// 修改原型对象中的push方法
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) {
        return originalPush.call(this, location, onResolve, onReject);
    }
    return originalPush.call(this, location).catch(err => err); // 捕获错误但不处理
};
Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Manager',
        component: () => import('../views/Manager.vue'),
        redirect: 'home',//localhost:7000/ 会重定向到 localhost:7000/home
        children: [
            {path: '403', name: 'Auth', meta: {name: '无权限'}, component: () => import('../views/manager/Auth.vue')},
            {path: 'home', name: "Home", meta: {name: '系统首页'}, component: () => import('../views/manager/Home.vue')},
            {path: 'user', name: "User", meta: {name: '用户信息'}, component: () => import('../views/manager/User.vue')},
            {
                path: 'person',
                name: "Person",
                meta: {name: '个人信息'},
                component: () => import('../views/manager/Person.vue')
            },
            {
                path: 'password',
                name: "Password",
                meta: {name: '修改密码'},
                component: () => import('../views/manager/Password.vue')
            },
            {
                path: 'equipment',
                name: "Equipment",
                meta: {name: '设备信息'},
                component: () => import('../views/manager/Equipment.vue')
            },
            {
                path: 'notice',
                name: "Notice",
                meta: {name: '系统公告'},
                component: () => import('../views/manager/Notice.vue')
            },
            {path: 'logs', name: "Logs", meta: {name: '系统日志'}, component: () => import('../views/manager/Logs.vue')},
            {
                path: 'charts',
                name: "Charts",
                meta: {name: '统计图表'},
                component: () => import('../views/manager/Charts.vue')
            },
            {
                path: 'goods',
                name: "Goods",
                meta: {name: '饮水下单'},
                component: () => import('../views/manager/Goods.vue')
            },
            {
                path: 'orders',
                name: "Orders",
                meta: {name: '订单'},
                component: () => import('../views/manager/Orders.vue')
            },
        ]
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            name: '登录',
        },
        component: () => import('../views/Login.vue')
    },
    {
        path: '/register',
        name: 'Register',
        meta: {
            name: '注册',
        },
        component: () => import('../views/Register.vue')
    },
    {
        path: '*',
        name: '404',
        meta: {
            name: '页面找不到',
        },
        component: () => import('../views/404.vue')
    }
]


const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


router.beforeEach((to, from, next) => {
    //to到哪去  from从哪来  next帮助跳转路由的函数
    let adminPaths = ['/user', '/equipment', '/notice', '/logs', '/charts']
    let user = JSON.parse(localStorage.getItem("base-user") || '{}')
    if (to.path === '/login' || to.path === '/register') {
        next()
    }
    else if (user && user.id){
            if (adminPaths.includes(to.path) && user.role !== '管理员') {
                next('/403');
            } else {
                next();
            }
        } else {
        next('/login')
    }
});


export default router
